package com.zcx.qshop.dialog;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.helper.view.hot.HotListView;
import com.zcx.qshop.R;
import com.zcx.qshop.conn.JsonAttributeAsyGet;
import com.zcx.qshop.conn.JsonGoodsattributeAsyGet;
import com.zcx.qshop.conn.JsonShopcartAsyGet;
import com.zcx.qshop.view.AttributeView;
import com.zcx.qshop.view.CalculatorView;

/**
 * Created by Administrator on 2015/8/31 0031.
 */
public abstract class CommodityAttributeDialog extends QSDialog implements View.OnClickListener {

    private SimpleDraweeView picurl;

    private TextView back, title, price, unit, stock, attributeOne, attributeTwo, addShop, buy;

    private AttributeView attributeViewOne, attributeViewTwo;

    private CalculatorView calculatorView;

    private JsonAttributeAsyGet.Info info = new JsonAttributeAsyGet.Info();

    private boolean isInfo = true;

    private JsonAttributeAsyGet jsonAttributeAsyGet = new JsonAttributeAsyGet("", "" , "", new AsyCallBack<JsonAttributeAsyGet.Info>(){

        @Override
        public void onSuccess(String toast, int type, JsonAttributeAsyGet.Info info) throws Exception {

            onAttribute(isInfo, CommodityAttributeDialog.this.info = info);

            price.setText("￥" + info.price);

            stock.setText("库存：" + info.stock);

            calculatorView.setClickable(true);

            if(checkStock(1)){

                onCountChange("1");

                calculatorView.setCount("1");

            }else{

                onCountChange("0");

                calculatorView.setCount("0");

            }

            isInfo = false;

        }

    });

    public CommodityAttributeDialog(final Context context, JsonGoodsattributeAsyGet.Info attribut, String gid, String type) {

        super(context);

        setContentView(R.layout.dialog_commodity_attribute);

        back = (TextView) findViewById(R.id.dialog_commodity_attribute_back);

        picurl = (SimpleDraweeView) findViewById(R.id.dialog_commodity_attribute_picurl);

        title = (TextView) findViewById(R.id.dialog_commodity_attribute_title);

        price = (TextView) findViewById(R.id.dialog_commodity_attribute_price);

        unit = (TextView) findViewById(R.id.dialog_commodity_attribute_unit);

        stock = (TextView) findViewById(R.id.dialog_commodity_attribute_stock);

        attributeOne = (TextView) findViewById(R.id.dialog_commodity_attribute_atttibute_one);

        attributeTwo = (TextView) findViewById(R.id.dialog_commodity_attribute_atttibute_two);

        attributeViewOne = (AttributeView) findViewById(R.id.dialog_commodity_attribute_atttibute_view_one);

        attributeViewTwo = (AttributeView) findViewById(R.id.dialog_commodity_attribute_atttibute_view_two);

        calculatorView = (CalculatorView) findViewById(R.id.dialog_commodity_attribute_calculator_view);

        calculatorView.setClickable(false);

        addShop = (TextView) findViewById(R.id.dialog_commodity_attribute_add_shop_car);

        buy = (TextView) findViewById(R.id.dialog_commodity_attribute_buy);

        picurl.setImageURI(Uri.parse(attribut.picurl));

        title.setText(Html.fromHtml(attribut.title));

        price.setText("￥" + (info.price = attribut.price));

        unit.setText("元/" + attribut.unit);

        stock.setText("库存：" + (info.stock = attribut.stock));

        attributeOne.setText(attribut.attributeone);

        attributeViewOne.loadList(attribut.attributeones);

        attributeTwo.setText(attribut.attributetwo);

        if(attribut.attributetwos.size() > 0){

            attributeViewTwo.loadList(attribut.attributetwos);

        }else{

            findViewById(R.id.dialog_commodity_attribute).setVisibility(View.GONE);

        }

        jsonAttributeAsyGet.gid = gid;

        attributeViewOne.setOnItemClickListener(new HotListView.OnItemClickListener<JsonGoodsattributeAsyGet.Info.Attribute>() {

            @Override
            public void onItemClick(View v, boolean isClick, JsonGoodsattributeAsyGet.Info.Attribute attribute) {

                jsonAttributeAsyGet.idone = attribute.id;

                if(isClick){

                    jsonAttributeAsyGet.execute(context);

                }

            }

        });

        attributeViewTwo.setOnItemClickListener(new HotListView.OnItemClickListener<JsonGoodsattributeAsyGet.Info.Attribute>() {

            @Override
            public void onItemClick(View v, boolean isClick, JsonGoodsattributeAsyGet.Info.Attribute attribute) {

                jsonAttributeAsyGet.idtwo = attribute.id;

                if(isClick){

                    jsonAttributeAsyGet.execute(context);

                }

            }

        });

        calculatorView.setOnCountChangeListener(null, new CalculatorView.OnCountChangeListener() {

            @Override
            public void onCountChange(JsonShopcartAsyGet.Info.Shop shop, CalculatorView calculatorView, int count) {

                if (checkStock(count)) {

                    calculatorView.setCount(count + "");

                    CommodityAttributeDialog.this.onCountChange(count + "");

                } else {

                    UtilToast.show(context, "库存不足");

                }

            }

        });

        addShop.setVisibility(type.equals("1") ? View.VISIBLE : View.INVISIBLE);

        back.setOnClickListener(this);

        addShop.setOnClickListener(this);

        buy.setOnClickListener(this);

        jsonAttributeAsyGet.execute(context);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        getWindow().setGravity(Gravity.BOTTOM);

    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){

            case R.id.dialog_commodity_attribute_back:

                dismiss();

                break;

            case R.id.dialog_commodity_attribute_add_shop_car:

                if(checkStock(calculatorView.getCount())){

                    onAddShopCarClick();

                }

                break;

            case R.id.dialog_commodity_attribute_buy:

                if(checkStock(calculatorView.getCount())){

                    onBuyClick();

                }

                break;

        }

    }

    private boolean checkStock(int count){

        try{

            if(info.stock >= count){

                return true;

            }else{

                UtilToast.show(getContext(), "库存不足");

                return false;

            }

        }catch (Exception e){

            return false;

        }

    }

    @Override
    public void show() {

        try{

            super.show();

        }catch (Exception e){}

    }

    @Override
    public void dismiss() {

        try{

            super.dismiss();

        }catch (Exception e){}

    }

    public abstract void onAttribute(boolean isInfo, JsonAttributeAsyGet.Info info);

    public abstract void onCountChange(String count);

    public abstract void onAddShopCarClick();

    public abstract void onBuyClick();

}
